﻿@model EC_ASS.Models.Feedback

@if (!(String.IsNullOrEmpty(ViewBag.CMSG)))
{
    <div class="alert alert-success">@ViewBag.CMSG</div>
}
else
{
    <form action="@Url.Action("EditFeedback", "Admin")" class="form-horizontal" 
                 style="margin-top: 20px;" id="editFeedbackForm" method="post">
        <div class="control-group">
            <label class="control-label" for="inputName">Tên</label>
            <div class="controls">
                <input type="text" id="inputName" name="Customer" value="@Model.Customer" required />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputEmail">Email</label>
            <div class="controls">
                <input type="text" id="inputEmail" value="@Model.Email" name="Email" required />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputContent">Nội dung</label>
           <div class="controls">
               <textarea name="Content" id="inputContent" rows="4">@Model.Content</textarea>
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <button type="submit" class="btn">Cập nhật</button>
            </div>
        </div>
    </form>
}
<!--Submit add form-->
<script>
    $(function () {
        $('#editFeedbackForm').submit(function (e) {
            e.preventDefault();
            $('#editLoading').show();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    $('#editLoading').hide();
                    $('#editModalContent').html('<div class="alert alert-success">Cập nhật thành công!</div>');
                    setTimeout(function () {
                        $('#editModal').modal('hide');
                    }, 2000);
                    setTimeout(function () {
                        $('#listFeedbackTable').load('@Url.Action("_ListFeedbackPage", "Admin")').hide().fadeIn('slow');
                    }, 2000);
                }
            });
            return false;
        });

        // Scrollbar
        var textArea = $(".content textarea");
        textArea.wrap("<div class='textarea-wrapper' />");
        var textAreaWrapper = textArea.parent(".textarea-wrapper");
        textAreaWrapper.mCustomScrollbar({
            scrollInertia: 0,
            advanced: { autoScrollOnFocus: false }
        });
        var hiddenDiv = $(document.createElement("div")),
            content = null;
        hiddenDiv.addClass("hiddendiv");
        $("body").prepend(hiddenDiv);
        textArea.bind("keyup", function (e) {
            content = $(this).val();
            var clength = content.length;
            var cursorPosition = textArea.getCursorPosition();
            content = "<span>" + content.substr(0, cursorPosition) + "</span>" + content.substr(cursorPosition, content.length);
            content = content.replace(/\n/g, "<br />");
            hiddenDiv.html(content + "<br />");
            $(this).css("height", hiddenDiv.height());
            textAreaWrapper.mCustomScrollbar("update");
            var hiddenDivSpan = hiddenDiv.children("span"),
                hiddenDivSpanOffset = 0,
                viewLimitBottom = (parseInt(hiddenDiv.css("min-height"))) - hiddenDivSpanOffset,
                viewLimitTop = hiddenDivSpanOffset,
                viewRatio = Math.round(hiddenDivSpan.height() + textAreaWrapper.find(".mCSB_container").position().top);
            if (viewRatio > viewLimitBottom || viewRatio < viewLimitTop) {
                if ((hiddenDivSpan.height() - hiddenDivSpanOffset) > 0) {
                    textAreaWrapper.mCustomScrollbar("scrollTo", hiddenDivSpan.height() - hiddenDivSpanOffset);
                } else {
                    textAreaWrapper.mCustomScrollbar("scrollTo", "top");
                }
            }
        });
        $.fn.getCursorPosition = function () {
            var el = $(this).get(0),
                pos = 0;
            if ("selectionStart" in el) {
                pos = el.selectionStart;
            } else if ("selection" in document) {
                el.focus();
                var sel = document.selection.createRange(),
                    selLength = document.selection.createRange().text.length;
                sel.moveStart("character", -el.value.length);
                pos = sel.text.length - selLength;
            }
            return pos;
        }
    });
</script>
